<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 14:05
 */

namespace app\admin\controller;


use app\admin\logic\SiteLogic;
use think\facade\Request;
use app\vehicle\model\Volunteer;
use app\vehicle\model\User;
use app\vehicle\model\Site as SiteModel;
use app\vehicle\model\SiteTeam;
class Site extends Base
{

    private $SiteLogic;

    public function __construct()
    {
        parent::__construct();
        $this->SiteLogic = new SiteLogic();
    }

    public function index(){
        return $this->fetch();
    }

    public function ajaxIndex(){
        $where = Request::param();
        $articleList = $this->SiteLogic->cateList($where, self::$nowPage);
        return json($articleList);
    }

    public function add(){
        $act = 'add';
        $id = Request::get('id');
        if ($id){
            $act = 'edit';
            $info = $this->SiteLogic->cateInfo($id);
            $this->assign('info', $info['data']);
        }
        $this->assign('act', $act);
        $this->assign('id', $id);
        $s = [];
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        if($site_id){
            $s[] = ['id','<>',$site_id];
        }
        $site = SiteModel::where($s)->select();
        $this->assign('site',$site);
        $all_id = Volunteer::column('user_id');
        return $this->fetch();
    }

    public function get_user(){
        $data = Request::param();
        if($data['type']==1){
            $role_id = explode(',',SiteModel::where('id','=',$data['id'])->value('role_id'));
        }else if($data['type']==3){
            $role_id = explode(',',SiteTeam::where('id','=',$data['id'])->value('pass_id'));
        }else{
            $role_id = explode(',',SiteModel::where('id','=',$data['id'])->value('pass_id'));
        }
        $list = User::field('nick_name as name,id as value')->order('id desc')->select();
        foreach($list as $k=>$v){
            if(in_array($v['value'],$role_id)){
                $list[$k]['selected'] = true;
            }
        }
        return json(['list'=>$list]);
    }

    public function handle(){
        $data = Request::param();
        $res = $this->SiteLogic->handle($data);
        return json($res);
    }

}